package com.transac.demo.service;

import com.transac.demo.mapper.Account;
import com.transac.demo.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransactionNeverServiceImpl implements TransactionNeverService{
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    TransactionNeverServiceImpl transactionNeverService;

    @Override
    @Transactional
    public void updateMoney1() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionNeverService.updateJerryMoney1();
    }

    @Transactional(propagation = Propagation.NEVER)
    public void updateJerryMoney1() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
    }

    @Override
    public void updateMoney2() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionNeverService.updateJerryMoney2();
    }

    @Transactional(propagation = Propagation.NEVER)
    public void updateJerryMoney2() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        throw new RuntimeException("error");
    }
}
